﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace AIC_PN
{
    public partial class FormBaoCaoMau3 : MyFromBase
    {
        private PrepareDataForTreeView DataForTreeView = new PrepareDataForTreeView();
        private static int IDDonVi;
        private static int KyBaoCao;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermission();
            lblNotice.Visible = false;
            if (!IsPostBack)
            {
                BindDataToDropDownList(); ;
                DataForTreeView.PopulateRootLevel(trvDonVi);
            }
            KyBaoCao = Convert.ToInt32(ddlKyBaoCao.SelectedValue);
        }

        protected void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("BaoCaoMau3");
            if (Role == null || Role["Xem"] == 0)
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }
        protected void trvDonVi_SelectedNodeChanged(object sender, EventArgs e)
        {
            lblDonVi.Text = "Đơn vị đã chọn : " + trvDonVi.SelectedNode.Text;
            IDDonVi = Convert.ToInt32(trvDonVi.SelectedValue);
        }

        protected void trvDonVi_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            DataForTreeView.PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
        }

        protected void BindDataToDropDownList()
        {
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT * FROM dbo.tblDanhMuc WHERE MaDM='KyBaoCao'");
            ddlKyBaoCao.DataSource = dt;
            ddlKyBaoCao.DataTextField = "TenDM";
            ddlKyBaoCao.DataValueField = "ID";
            ddlKyBaoCao.DataBind();
        }

        protected void btnXemBaoCao_Click(object sender, EventArgs e)
        {
            if (trvDonVi.SelectedNode == null)
            {
                lblNotice.Visible = true;
                lblNotice.InnerText = "Vui lòng lựa chọn đơn vị muốn xem báo cáo";
            }
            else
            {
                List<SqlParameter> lstParams = new List<SqlParameter>();

                if (chkTongHop.Checked)
                {
                    lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                    lstParams.Add(SqlHelper.CreateParameterObject("@parentID", SqlDbType.NVarChar, ParameterDirection.Input, IDDonVi + ";"));
                    lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                    MyReportSQL = "SELECT A.ID,B.TenDM AS DMCha,A.TenDM,SUM(F1) AS F1,SUM(F2) AS F2,SUM(F3) AS F3,SUM(F4) AS F4,A.STT,A.DVT FROM dbo.tblBaoCaoMau3 M1 " +
                                    "INNER JOIN dbo.tblDanhMucBaoCao A ON M1.TenBaoCaoID_DM = A.ID " +
                                    "INNER JOIN dbo.tblDanhMucBaoCao B ON A.FatherID = B.ID " +
                                    "WHERE #REPLACE# AND KyBaoCaoID_DM = @KyBaoCao AND DonViID IN ((SELECT ID FROM tblDonVi WHERE parentID LIKE '%' + @parentID + '%' )) " +
                                    "GROUP BY A.ID,B.TenDM,A.TenDM,A.STT,A.DVT ORDER BY A.ID";
                }
                else
                {
                    lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                    lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                    MyReportSQL = "SELECT A.ID,B.TenDM AS DMCha,A.TenDM,F1,F2,F3,F4,A.STT,A.DVT,A.FatherID FROM dbo.tblBaoCaoMau3 M1 " +
                                    "INNER JOIN dbo.tblDanhMucBaoCao A ON M1.TenBaoCaoID_DM = A.ID " +
                                    "INNER JOIN dbo.tblDanhMucBaoCao B ON A.FatherID = B.ID " +
                                    "WHERE #REPLACE# AND KyBaoCaoID_DM = @KyBaoCao AND DonViID = @DonViID";

                }
                MyReportParams = lstParams;
                MyReportSQLParam = "SELECT BC.CM_TenBaoCao,BC.CM_ThoiGianBaoCao,DV.TenDV,BC.CM_NguoiLapBieu,BC.CM_ThuTruong FROM dbo.tblBaoCaoMau3 BC ,tblDonVi DV " +
                                   "WHERE BC.DonViID = DV.ID AND BC.KyBaoCaoID_DM = @KyBaoCao AND BC.DonViID = @DonViID AND BC.IsCommon = 1";
                string UnionParam = " UNION ALL SELECT '',CONVERT(NVARCHAR(10),GETDATE(),103),N'" + trvDonVi.SelectedNode.Text + "','','' WHERE NOT EXISTS (" + MyReportSQLParam + ")";
                MyReportSQLParam += UnionParam;
                MyReportName = "rpt01.rpt";
                MyTableName = "tblBaoCaoMau3";
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), null, "window.open('MyReportViewMau3.aspx');", true);


            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("index.aspx");
        }
    }
}
